DAY37:Find the odd int


Posted by birdbirdmurmur on 2023-08-19

題目連結

https://www.codewars.com/kata/54da5a58ea159efa38000836

解法

function findOdd(A) {
    let count = {}
    let min = Infinity

    for (const num of A) {
        count[num] ? count[num]++ : count[num] = 1
    }

    for (const num in count) {
        if (count[num] % 2 !== 0 && min > parseInt(num)) {
            min = parseInt(num)
        }
    }

    return min === Infinity ? 0 : min
}

筆記

同時練習for...offor...in兩種用法

  1. for...of用於迭代陣列等可迭代物件的
  2. for...in用於迭代物件的屬性,包括可數的屬性。

#javascript #Codewars #for...in #for...of #object







Related Posts

使用 Javascript 取得元素的座標

使用 Javascript 取得元素的座標

DEC 14 2023 PROCL-5: User does not have permission to perform a local registry operation on this key. Authentication error [User [root] [已解決]

DEC 14 2023 PROCL-5: User does not have permission to perform a local registry operation on this key. Authentication error [User [root] [已解決]

Web開發學習筆記13 —   DOM節點操作屬性與方法

Web開發學習筆記13 — DOM節點操作屬性與方法


Comments